﻿#region

using System;

#endregion

namespace SharpObservation
{
    /// <summary>
    /// Base class for implementing the an object which exposes events and implements the disposable pattern
    /// </summary>
    /// <remarks>
    /// Derive from this class, to implement the disposable pattern correctly.
    /// </remarks>
    public abstract class DisposableObject : IDisposable
    {
        private bool isDisposed;
        private Action throwIfDisposed =  () => { };

        /// <summary>
        ///   Gets whether the factory instance has been disposed.
        /// </summary>
        public bool IsDisposed { get { return isDisposed; } }

        /// <summary>
        ///   Checks whether the factory instance has been disposed, and throws an ObjectDisposedException if it has
        /// </summary>
        /// <exception cref = "ObjectDisposedException">Thrown if instance has been disposed</exception>
        protected Action ThrowIfDisposed { get { return throwIfDisposed; } }

        /// <summary>
        /// Releases resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            // this method could be abstract, but is deliberately implemented to support refactoring...
            // derived classes should always call base.Dispose.
        }

        /// <summary>
        ///   Disposes the factory instance
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
        public void Dispose()
        {
            DisposeHelper.DisposeOf(this, true);
        }

        ///// <summary>
        /////   Finalizes the factory instance. Should never be called.
        ///// </summary>
        //[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
        //~DisposableObject()
        //{
        //    DisposeHelper.DisposeOf(this, false);
        //}
    }
}